<?php

namespace App\Http\Services\FormBuilder\Elements;

class TextArea extends FormControl
{
    protected $attributes = [
        'rows' => 10,
        'cols' => 50,
        'class' => 'form-control',
    ];
    public $fillable = [
        'rows', 'cols','id', 'name','class', 'placeholder'
    ];

    protected $value;

    public function render()
    {
        $this->value($this->getAttribute('value'));
        if ($this->label) {
            $element        = clone $this;
            $element->label = false;

            return (new Field($this->label, $element))->render();
        }

        $result = '<textarea';
        $result .= $this->renderAttributes();
        $result .= '>';
        $result .= $this->value;
        $result .= '</textarea>';

        return $result;
    }

    public function rows($rows)
    {
        $this->setAttribute('rows', $rows);

        return $this;
    }

    public function cols($cols)
    {
        $this->setAttribute('cols', $cols);

        return $this;
    }

    public function value($value)
    {
        $this->value = $value;

        return $this;
    }

    public function placeholder($placeholder)
    {
        $this->setAttribute('placeholder', $placeholder);

        return $this;
    }

    public function defaultValue($value)
    {
        if (!$this->hasValue()) {
            $this->value($value);
        }

        return $this;
    }

    protected function hasValue()
    {
        return isset($this->value);
    }

    public function detailBlock()
    {
        $class = $this->getAttribute('li_class');
        $displayName = $this->getAttribute('display_name');
        $value = $this->getAttribute('value');

        $html = '';
        $html .= '<div class="items '.$class.'">';
        $html .= '<label class="col-12">'.$displayName.'</label>&nbsp';
        $html .= '<div class="detail col-10">'.nl2br($value).'</div>';
        $html .='</div>';

        return $html;
    }
}
